package GUI;

import UserData.Schedule;
import UserData.User_Info;
import UserData.User_Management;


import com.toedter.calendar.JDateChooser;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Date;

import javafx.geometry.Point2D;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class Compare extends JPanel {
	private static final int ArrayList = 0;
	private JPanel panelCompare;
	private JScrollPane scrollPaneShowFriends;
	private JScrollBar scrollBar;
	private JLabel lblMyFriends;
	private JLabel lblTerm;
	private JDateChooser endDateChooser;
	private JDateChooser startDateChooser;
	private JButton btnEnter;
	private JLabel lblStartDay;
	private JLabel lblEndday;
	private JCheckBox[] checkBoxFriend;
	private JTextField txtfEntername;
	private JButton btnEntername;
	private int UserListNumber = User_Management.Get_User_list.size();
	private int ResultCount[][]=new int[26][];
	private Result result;
	
	//double x,y;
	//Point2D point = new Point2D(x,y);
	//private Point2D point = new Point2D(0,0);
	/*Point2D point = new Point2D(0,0);
	private ArrayList<Point2D> Point_List = new ArrayList();
	*/
	
	int point_X=0;
	int point_Y=0;
	
	ArrayList<Integer> Point_X_List = new ArrayList();
	ArrayList<Integer> Point_Y_List = new ArrayList();

	public Compare()
	{
		setBounds(0, 0, 745, 489);
		setLayout(null);

		this.panelCompare = new JPanel();

		this.panelCompare.setBounds(0, 0, 745, 489);
		add(this.panelCompare);
		this.panelCompare.setLayout(null);

		
		
		for(int i = 0; i<26; i++){
			
			ResultCount[i]=new int[31];
			
		}
		
	
			
		this.scrollPaneShowFriends = new JScrollPane();
		this.scrollPaneShowFriends.setBounds(47, 53, 283, 380);
		this.scrollBar = new JScrollBar();
		this.lblMyFriends = new JLabel("My Friends");
		this.lblMyFriends.setBounds(47, 53, 137, 35);
		this.lblTerm = new JLabel("Term");
		this.lblTerm.setBounds(212, 53, 63, 35);
		this.btnEnter = new JButton("Enter")
		{
			public void paintComponent(Graphics g) {
				Image image = new ImageIcon("iconenter.png").getImage();
				g.drawImage(image, 0, 0, this);
			}
		};
		this.btnEnter.setBounds(574, 343, 80, 40);
		this.btnEnter.setBorder(null);
		this.lblStartDay = new JLabel("StartDay");
		lblStartDay.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		this.lblStartDay.setBounds(342, 82, 57, 15);
		this.lblEndday = new JLabel("EndDay");
		lblEndday.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		this.lblEndday.setBounds(342, 160, 57, 15);
		this.endDateChooser = new JDateChooser();
		this.endDateChooser.setBounds(342, 175, 220, 40);

		this.endDateChooser.getCalendarButton().setBorder(null);
		this.endDateChooser.setBorder(null);
		this.startDateChooser = new JDateChooser();
		this.startDateChooser.setBounds(342, 97, 220, 40);

		this.startDateChooser.getCalendarButton().setBorder(null);
		this.startDateChooser.setBorder(null);

		this.startDateChooser.getCalendarButton().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
			}
		});
		this.panelCompare.add(this.scrollPaneShowFriends);

		this.scrollPaneShowFriends.setRowHeaderView(this.scrollBar);

		JPanel panel_1 = new JPanel();
		this.scrollPaneShowFriends.setViewportView(panel_1);
		panel_1.setLayout(null);

		User_Management userManage = new User_Management();

		checkBoxFriend = new JCheckBox[UserListNumber]; //친구 List 체크박스 회원 수만큼의 사이즈로 초기화

		//Creating all Friends' List
		for(int i = 0; i<UserListNumber; i++){   	
			checkBoxFriend[i] = new JCheckBox(User_Management.Get_User_list.get(i).getUser_Real_Name());
			//Check If there is Log in User's Name. If there is, Do not insert that user's name at friend's list.
			if(checkBoxFriend[i].getText()!=User_Management.Now_User.getUser_Real_Name()){
				checkBoxFriend[i].setBounds(0, 25*i, 118, 23);
				panel_1.add(checkBoxFriend[i]);   
			}
		}

		//making each user's time united.
		ArrayList<Schedule[][]> allUserSchedule = new ArrayList<Schedule[][]>();
		/*for(int i = 0; i<UserListNumber; i++){
    	if(checkBoxFriend[i].isSelected()){
    		for(Schedule[] sc : User_Management.Get_User_list.get(i).getSche_List()){   			
    			allUserSchedule.add(sc);
    		}
    	}
    }*/

		//creating result schedule    
		for(Schedule[][] e: allUserSchedule){
			for(int i = 0; i<26; i++){
				for(int j = 0; j<31; j++){
					if(User_Management.Now_User.Sche_List[i][j]!=null){
						ResultCount[i][j]=+1; //Counting How many appointments is there in time [i][j]
					}
				}
			}
		}
		
		
		for(int i=0;i<26;i++){
			for(int j=0;j<31;j++){
				if(ResultCount[i][j]==0){
					point_X=i;
					point_Y=j;
					Point_X_List.add(point_X);
					Point_Y_List.add(point_Y);
					
				}
		
		}
		
			
			
		}//System.out.println("resultcount size :   "+ Point_X_List.size());
		result = new Result();
		result.setXlist(Point_X_List);
		result.setYlist(Point_Y_List);
		System.out.println(result.xlist.size());
		System.out.println(result.ylist.size());
		
		
		result.apprearDay();
		
		
		
		this.lblMyFriends.setForeground(Color.DARK_GRAY);
		this.lblMyFriends.setFont(new Font("占쏙옙占쏙옙 占쏙옙占�", 0, 20));
		this.panelCompare.add(this.lblMyFriends);

		this.lblTerm.setForeground(Color.DARK_GRAY);
		this.lblTerm.setFont(new Font("占쏙옙占쏙옙 占쏙옙占�", 0, 20));
		this.panelCompare.add(this.lblTerm);

		this.btnEnter.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) {
				Compare.this.EnterActionPerformed(e);
			}
		});
		this.panelCompare.add(this.btnEnter);
		this.panelCompare.add(this.lblStartDay);
		this.panelCompare.add(this.lblEndday);
		this.panelCompare.add(this.startDateChooser);
		this.panelCompare.add(this.endDateChooser);

		this.btnEntername = new JButton("New button") {
			public void paintComponent(Graphics g) {
				Image image = new ImageIcon("fapp.png").getImage();
				g.drawImage(image, 0, 0, this);
			}
		};
		this.btnEntername.setBorder(null);

		this.btnEntername.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			//	System.out.println("txtf");
				Compare.this.txtfEntername = new JTextField();
				Compare.this.txtfEntername.setBackground(new Color(255, 255, 255));
				Compare.this.txtfEntername.setBorder(null);
				Compare.this.txtfEntername.setToolTipText("appointment name");
				Compare.this.txtfEntername.setText("");
				Compare.this.txtfEntername.setDisabledTextColor(Color.WHITE);
				Compare.this.txtfEntername.setForeground(Color.LIGHT_GRAY);
				Compare.this.txtfEntername.setBounds(342, 343, 220, 40);
				Compare.this.panelCompare.add(Compare.this.txtfEntername);
				Compare.this.txtfEntername.setColumns(10);

				Compare.this.panelCompare.remove(Compare.this.btnEntername);
			}
		});
		this.btnEntername.setBounds(342, 343, 220, 40);
		this.panelCompare.add(this.btnEntername);
	}

	public void EnterActionPerformed(ActionEvent e) {

		int FriendNotCheckedNum = 0;
		//Check If there is no selected Checkbox
		for (int i = 0; i < UserListNumber; i++) {
			if (this.checkBoxFriend[i].isSelected() == false) {
				FriendNotCheckedNum++;
			}
		}

		if ((this.startDateChooser.getDate() == null)
				|| (this.startDateChooser.getDate().equals(""))) {
			JOptionPane.showMessageDialog(this, "Type your Start Date");
		} else if ((this.endDateChooser.getDate() == null)
				|| (this.endDateChooser.getDate().equals(""))) {
			JOptionPane.showMessageDialog(this, "Type your End Date");
		} else if (FriendNotCheckedNum == UserListNumber) {
			JOptionPane.showMessageDialog(this,
					"You have to select at least One Friend");
		} else {
			Result regi = new Result();
			regi.setVisible(true);
		}
	}
	
	
	}